package com.riip.demos.elasticjob.config;


import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticJobConfig {

	@Autowired
	private ZookeeperRegistryCenter regCenter;

//    @Bean(initMethod = "init")
//    public JobScheduler msgSend30sJobScheduler(final MsgSend30sJob job, @Value("${msgSendJob.cron}") final String cron, @Value("${msgSendJob.shardingTotalCount}") final int shardingTotalCount,
//                                               @Value("${msgSendJob.shardingItemParameters}") final String shardingItemParameters) {
//        return new SpringJobScheduler(job, regCenter, getLiteJobConfiguration(job.getClass(), cron, shardingTotalCount, shardingItemParameters));
//    }
//
//    @Bean(initMethod = "init")
//    public JobScheduler msgSend24hJobScheduler(final MsgSend24hJob job, @Value("${msgSendJob.cron}") final String cron, @Value("${msgSendJob.shardingTotalCount}") final int shardingTotalCount,
//                                               @Value("${msgSendJob.shardingItemParameters}") final String shardingItemParameters) {
//        return new SpringJobScheduler(job, regCenter, getLiteJobConfiguration(job.getClass(), cron, shardingTotalCount, shardingItemParameters));
//    }
//
//    @Bean(initMethod = "init")
//    public JobScheduler stopAlarmJobScheduler(final StopAlarmJob job, @Value("${msgSendJob.cron}") final String cron, @Value("${msgSendJob.shardingTotalCount}") final int shardingTotalCount,
//                                              @Value("${msgSendJob.shardingItemParameters}") final String shardingItemParameters) {
//        return new SpringJobScheduler(job, regCenter, getLiteJobConfiguration(job.getClass(), cron, shardingTotalCount, shardingItemParameters));
//    }

	private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
		return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
			jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
	}
}
